package com.itheima.controller;

import com.itheima.pojo.Blog;
import com.itheima.pojo.Tag;
import com.itheima.service.BlogService;
import com.itheima.service.BlogTagService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 博客标签相关接口
 */
@RestController
@RequestMapping("/tag")
@Api(tags = "博客标签相关接口")
@CrossOrigin
public class TagController {
    @Autowired
    BlogService blogService;
    @Autowired
    BlogTagService blogTagService;

    @GetMapping("/getArticleByTagUid/{tagUid}/{currntPage}/{pageSize}")
    @ApiOperation("通过TagUid获取文章")
    public String getArticleByTagUid(@PathVariable String tagUid,@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        if(tagUid==null||"".equals(tagUid)){
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST,"请输入标签");
        }
        try{
            List<Blog> blogList  = blogTagService.getArticleByTagUid(tagUid, currentPage, pageSize);
            return ResultUtil.result(HttpServletResponse.SC_OK,blogList);
        } catch (Exception e){
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.GET_ARTICLE_BY_TAGUID_FAIL);
        }
    }

    @GetMapping("/getTagList")
    @ApiOperation("获取标签信息")
    public String getTagList(){
        try{
            List<Tag> tagList = blogTagService.getTagList();
            return ResultUtil.result(HttpServletResponse.SC_OK,tagList);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST,MessageConstant.TAG_FINDALL_FAIL);
        }
    }
}
